package com.lmc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmc.common.R;
import com.lmc.mapper.OrderMapper;
import com.lmc.pojo.Order;
import com.lmc.pojo.Wallet;
import com.lmc.service.OrderService;
import com.lmc.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
@Transactional //涉及多表操作，添加事务
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private WalletService walletService;

    //假设从浏览器中拿到用户登录的id
    String userId = "147563212344";

    /**
     * 用户消费100元的接口
     *
     * @param order
     * @return
     */
    @Override
    public synchronized R<String> spendMoney(Order order) {
        //创建钱包对象
        LambdaQueryWrapper<Wallet> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotBlank(userId), Wallet::getUserId, userId);
        Wallet wallet = walletService.getOne(lqw);
        BigDecimal spendAmount = order.getAmount();

        //如果用户钱包金额不足，则返回错误提示
        if (spendAmount != null && spendAmount.compareTo(wallet.getAmount()) > 0) {
            return R.error("金额不足,请充值!");
        }
        //如果订单状态为: 1 (待支付)
        if (order.getStatus() != 1) {
            return R.error("您已付款,请勿重复提交！");
        }

        //用户付款成功   对应的钱包金额发生变化   订单及订单详情表发生变化
        LambdaUpdateWrapper<Wallet> luw = new LambdaUpdateWrapper<>();
        luw.eq(Wallet::getUserId, userId);
        luw.set(Wallet::getAmount, wallet.getAmount().subtract(spendAmount));
        boolean updateWallet = walletService.update(luw);//更新钱包余额

        //如果更新钱包成功，则更新订单状态
        if (updateWallet) {
            order.setStatus(2);//2:待派送状态
            this.updateById(order);
            return R.success("消费成功!");
        }

        return R.error("操作失败!");
    }

    /**
     * 用户退款20元接口
     *
     * @param order
     * @return
     */
    @Override
    public synchronized R<String> refundMoney(Order order) {

        //如果用户订单状态非已付款状态，无法申请退款
        if (order.getStatus() != 4) { //订单交易完成状态
            return R.error("您无法申请退款!");
        }

        //创建钱包对象
        LambdaQueryWrapper<Wallet> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotColumnName(userId), Wallet::getUserId, userId);
        Wallet wallet = walletService.getOne(lqw);
        BigDecimal refundAmount = order.getAmount();

        //定义一个变量记录,判断退款是否成功
        boolean flag = false;
        //用户退款   对应的钱包金额发生变化   订单及订单详情表发生变化
        LambdaUpdateWrapper<Wallet> luw = new LambdaUpdateWrapper<>();
        luw.eq(Wallet::getUserId, userId);
        luw.set(Wallet::getAmount, wallet.getAmount().add(refundAmount));
        flag = walletService.update(luw);

        //如果退款成功，则更新订单状态
        if (flag) {
            order.setStatus(5);//5:退款成功
            flag = this.updateById(order);
            if (flag){
                return R.success("退款成功!");
            }
        }
        return R.error("操作失败!");
    }

    //注：此处省略对库存的操作

}
